package com.nineclock.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.model.dto.SysCompanyUserDTO;
import com.nineclock.model.dto.SysRegisterDTO;
import com.nineclock.model.dto.SysUserDTO;
import com.nineclock.model.pojo.SysCompanyUser;
import com.nineclock.model.pojo.SysUser;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Override
    public SysUserDTO queryUser(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername,username).or().eq(SysUser::getMobile,username);

        SysUser user = sysUserMapper.selectOne(queryWrapper);
        SysUserDTO sysUserDTO = BeanHelper.copyProperties(user,SysUserDTO.class);
        return sysUserDTO;

    }
    @Autowired
    SysCompanyUserMapper sysCompanyUserMapper;

    @Override
    public SysCompanyUserDTO findUserById(Long userId) {
        if (userId==null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        SysCompanyUser sysCompanyUser = sysCompanyUserMapper.selectById(userId);
        if (sysCompanyUser!=null) {
            return BeanHelper.copyProperties(sysCompanyUser, SysCompanyUserDTO.class);
        }
        return null;

    }

    @Autowired
    SmsService smsService;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Override
    public Long register(SysRegisterDTO sysRegisterDTO) {
        if (sysRegisterDTO==null||sysRegisterDTO.getCheckcode()==null||sysRegisterDTO.getMobile()==null||sysRegisterDTO.getPassword()==null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        String realCode = redisTemplate.boundValueOps(SMSConstant.SMS_TYPE_REGISTER + sysRegisterDTO.getMobile()).get();
        if (!realCode.equals(sysRegisterDTO.getCheckcode())) {
            throw new NcException(ResponseEnum.INVALID_VERIFY_CODE);
        }
        SysUser sysUser = new SysUser();
        sysUser.setMobile(sysRegisterDTO.getMobile());
        sysUser.setPassword(new BCryptPasswordEncoder().encode(sysRegisterDTO.getPassword()));
        sysUser.setStatus((short) 1);
        sysUser.setCreateTime(new Date());
        sysUserMapper.insert(sysUser);

        //删除redis验证码
        smsService.deleteSmsCode(sysRegisterDTO.getMobile(),SMSConstant.SMS_TYPE_REGISTER);

        return sysUser.getId();

    }


}
